// Fill out your copyright notice in the Description page of Project Settings.


#include "MySQLSubsystem.h"
#include "../GConfig/GConfigFunctionLibrary.h"
#include "MySQLBPLibrary.h"
#include "../SGameGameInstance.h"

void UMySQLSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
	Super::Initialize(Collection);
}

void UMySQLSubsystem::Deinitialize()
{
	Super::Deinitialize();
}

void UMySQLSubsystem::MySubsystemTest()
{
	UE_LOG(LogTemp, Warning, TEXT("asd"));
}

void UMySQLSubsystem::JoinDB()
{
	auto IP = UGConfigFunctionLibrary::GetDBServerIP();
	auto Port = UGConfigFunctionLibrary::GetDBServerPort();
	FString UserID = "root";
	FString Password = "123456";
	UMySQLBPLibrary::SetConnectionProperties(IP,"sgame",UserID,Password,"",false);
}

FLoginMessage UMySQLSubsystem::LoginDetection(FLoginMessage Message)
{
	FLoginMessage ReturnMessage;
	ReturnMessage.Account = "err";
	ReturnMessage.Password = "err";
	ReturnMessage.ReturnID = -1;

	if (Message.Account.IsEmpty()&&Message.Password.IsEmpty())
	{
		Message.Account.Empty();
		return ReturnMessage;
	}
	//当前状态
	EMysqlSuccessStatus SuccessStatus= EMysqlSuccessStatus::Busy;
	//错误信息
	FString ErrorMessage;
	//数据表
	TArray<FMySQLDataTable> ResultByColumn; 
	//行数据
	TArray<FMySQLDataRow> ResultByRow;

	//检测是否链接数据库
	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage)!= EMySQLConnectionState::Open)
	{
		return ReturnMessage;
		//没有链接数据库
	}
	


	FString Query = "SELECT logindb.playerid,logindb.username,logindb.password FROM logindb WHERE '"+Message.Account+"' = logindb.username AND '"+ Message.Password+"' = logindb.password";
	UMySQLBPLibrary::SelectDataFromQuery(Query, SuccessStatus, ErrorMessage, ResultByColumn, ResultByRow);
	if (SuccessStatus== EMysqlSuccessStatus::Success)
	{
		if (ResultByRow.Num()==1)
		{
			ReturnMessage.ReturnID =FCString::Atoi(*ResultByRow[0].RowData[0]);
			ReturnMessage.Account = ResultByRow[0].RowData[1];
			ReturnMessage.Password = ResultByRow[0].RowData[0];
		}
		//报错信息 数据库异常 账号重复
		return ReturnMessage;
	}
	//报错
	// 数据库查询失败
	return ReturnMessage;

}

bool UMySQLSubsystem::CanRegisterPlayerName(FString Message)
{
	//当前状态
	EMysqlSuccessStatus SuccessStatus = EMysqlSuccessStatus::Busy;
	//错误信息
	FString ErrorMessage;
	//数据表
	TArray<FMySQLDataTable> ResultByColumn;
	//行数据
	TArray<FMySQLDataRow> ResultByRow;

	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage) != EMySQLConnectionState::Open)
	{
		return false;
		//没有链接数据库
	}

	FString Query = "SELECT logindb.playerid,logindb.username FROM logindb WHERE '" + Message + "' = logindb.username " ;
	UMySQLBPLibrary::SelectDataFromQuery(Query, SuccessStatus, ErrorMessage, ResultByColumn, ResultByRow);
	if (SuccessStatus == EMysqlSuccessStatus::Success)
	{
		if (ResultByRow.Num()==0)
		{
			return true;
		}
		return false;
	}
	return false;
}

void UMySQLSubsystem::RegisterAccount(FLoginMessage Message)
{
//UpdateDataFromQuery(FString Query, EMysqlSuccessStatus &SuccessStatus, FString &ErrorMessage);
	//增加语句
	FString Query = "INSERT INTO logindb(username,password) VALUES ('"+Message.Account+"','"+Message.Password+ "')";
	//增加状态
	EMysqlSuccessStatus SuccessStatus;
	//错误信息
	FString ErrorMessage;

	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage) != EMySQLConnectionState::Open)
	{
		return;
		//没有链接数据库
	}

	UMySQLBPLibrary::UpdateDataFromQuery(Query, SuccessStatus, ErrorMessage);
	if (SuccessStatus!= EMysqlSuccessStatus::Success)
	{
		//返回失败事件
		return;
	}
	Cast<USGameGameInstance>(GetGameInstance())->m_RegisterAccountSuccess.Broadcast();
	//返回成功事件
}

bool UMySQLSubsystem::IsPlayerHaveReadyInfo(int32 PlayerID)
{
	
	//当前状态
	EMysqlSuccessStatus SuccessStatus = EMysqlSuccessStatus::Busy;
	//错误信息
	FString ErrorMessage;
	//数据表
	TArray<FMySQLDataTable> ResultByColumn;
	//行数据
	TArray<FMySQLDataRow> ResultByRow;

	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage) != EMySQLConnectionState::Open)
	{
		return false;
		//没有链接数据库
	}

	FString Query = "SELECT readydb.playerid,readydb.playername FROM readydb WHERE '" +FString::FromInt(PlayerID)+ "' = readydb.playerid ";
	UMySQLBPLibrary::SelectDataFromQuery(Query, SuccessStatus, ErrorMessage, ResultByColumn, ResultByRow);
	if (SuccessStatus == EMysqlSuccessStatus::Success)
	{
		if (ResultByRow.Num() == 0)
		{
			return false;
		}
		return true;
	}
	return false;
}

void UMySQLSubsystem::WarehousingReadyInfo(FReadyInfo Message)
{
	FString Query = "INSERT INTO readydb(playerid,playername) VALUES ('" + FString::FromInt(Message.PlayerID) + "','" + Message.PlayerName.ToString() + "')";
	//增加状态
	EMysqlSuccessStatus SuccessStatus;
	//错误信息
	FString ErrorMessage;

	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage) != EMySQLConnectionState::Open)
	{
		return;
		//没有链接数据库
	}

	UMySQLBPLibrary::UpdateDataFromQuery(Query, SuccessStatus, ErrorMessage);
	if (SuccessStatus != EMysqlSuccessStatus::Success)
	{
		//返回失败事件
		return;
	}
	Cast<USGameGameInstance>(GetGameInstance())->m_RegisterAccountSuccess.Broadcast();
	//返回成功事件v

}

FReadyInfo UMySQLSubsystem::QueryPlayerInfoFromDB(int32 PlayerID)
{
	FReadyInfo Message;
	//当前状态
	EMysqlSuccessStatus SuccessStatus = EMysqlSuccessStatus::Busy;
	//错误信息
	FString ErrorMessage;
	//数据表
	TArray<FMySQLDataTable> ResultByColumn;
	//行数据
	TArray<FMySQLDataRow> ResultByRow;

	if (UMySQLBPLibrary::CheckConnectionState(ErrorMessage) != EMySQLConnectionState::Open)
	{
		return Message;
		//没有链接数据库
	}

	FString Query = "SELECT readydb.playerid,readydb.playername FROM readydb WHERE '" + FString::FromInt(PlayerID) + "' = readydb.playerid ";
	UMySQLBPLibrary::SelectDataFromQuery(Query, SuccessStatus, ErrorMessage, ResultByColumn, ResultByRow);
	if (SuccessStatus == EMysqlSuccessStatus::Success)
	{
		
		FReadyInfo RetutnMessage;
		if (ResultByRow.Num() == 0)
		{
			return Message;
		}
		Message.PlayerID=FCString::Atoi(*ResultByRow[0].RowData[0]);
		Message.PlayerName=FName(*ResultByRow[0].RowData[1]);
		return Message;
	}
	return Message;
}

//添加 INSERT INTO logindb(username,password) VALUES ("asd","dsa")